// pages/activity/details/details.js
import {
  $wuxDialog
} from '../../../miniprogram_npm/wux-weapp/index.js';
import {
  getActivity,
  getActivityGuestList,
  getMyActivityDetail

} from '../../../api/activity.js'
import {
  getSubscribeMessageAuth,
  debounce
} from '../../../utils/util.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    isLogin: false,
    isIOS: false,
    index: 0,
    isMine: false,
    id: '',
    activePictures: [],
    address: '',
    applyNum: 0,
    cover: '',
    deadline: '',
    maxNum: 0,
    name: '',
    planOfAction: '',
    price: 0,
    priceExplain: '',
    startTime: '',
    theme: '',
    ownerList: [],
    doList: [],
    assitList: [],
    guestList: [],
    myActivity: {},
    tagStyle: {
      p: 'line-height:24px;font-size:16px;color:#666666;letter-spacing:1px;margin-bottom:15rpx;',
      img: 'width:100vw;height:auto;',
    },
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.data.id = options.id;
    console.log('options',options)
    this.setData({
      isMine: options.isMine === '1',
      isLogin: getApp().globalData.isLogin,
      isIOS: getApp().globalData.isIOS,
    })
    if (this.data.isMine) {
      this.getMyActivity()
    }
    this.getGuestList();
    this.getData();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.data.isLogin = getApp().globalData.isLogin;
  },

  alert() {
    $wuxDialog().alert({
      resetOnClose: true,
      title: '费用明细',
      content: this.data.priceExplain,
      onConfirm(e) {},
    })
  },
  getMyActivity() {
    getMyActivityDetail(this.data.id).then(res => {
      this.setData({
        myActivity: res
      })
    })
  },
  getGuestList() {
    getActivityGuestList(this.data.id).then(res => {
      this.setData({
        guestList: this.data.guestList.concat(res),

      })
    })
  },
  getData() {
    getActivity(this.data.id).then(res => {
      console.log('xiangqing',res)
      for (let i = 0; i < res.organizationListVOList.length; i++) {
        if (res.organizationListVOList[i].type === 0) {
          this.data.ownerList.push(res.organizationListVOList[i])
        } else if (res.organizationListVOList[i].type === 1) {
          this.data.doList.push(res.organizationListVOList[i])

        } else if (res.organizationListVOList[i].type === 2) {
          this.data.assitList.push(res.organizationListVOList[i])
        }
      }
      this.setData({
        activePictures: res.activePictures.split(','),
        address: res.address,
        applyNum: res.applyNum,
        deadline: res.deadline,
        maxNum: res.maxNum,
        name: res.name,
        planOfAction: res.planOfAction,
        price: res.price,
        priceExplain: res.priceExplain,
        startTime: res.startTime,
        theme: res.theme,
        ownerList: this.data.ownerList,
        doList: this.data.doList,
        assitList: this.data.assitList,

      })
    })
  },

  /**
   * 跳转至报名页面
   */
  async jumpToEnroll() {
    await this.isLogin();

    // 申请活动报名通知
    await getSubscribeMessageAuth(['FJ5iaSipTuOH8b3Q8j50ZNTmDZXJ9qsl8XjrpodjcvA']);

    debounce(() => {
      wx.navigateTo({
        url: `/pages/activity/enroll/enroll?id=${this.data.id}&price=${this.data.price}`,
      })
    }, 1000, true);
  },

  handleScrollBottom(e) {
    if (this.index === 1) {
      if (this.data.rowCount > this.data.getGuestList.length) {
        this.getGuestList();
      }
    }

  },
  /**
   * tabs 切换
   */
  handleTabsChange(e) {
    const {
      key
    } = e.detail;
    const index = parseInt(key);
    this.setData({
      key,
      index,
    })
  },

  /**
   * swiper 切换
   */
  handleSwiperChange(e) {
    const {
      current
    } = e.detail
    const key = current.toString();
    this.setData({
      key,
      index: current,
    })
  },

  /**
   * 判断是否登录
   */
  isLogin() {
    return new Promise(resolve => {
      if (this.data.isLogin) {
        resolve()
      } else {
        $wuxDialog().confirm({
          title: '你还没有登陆哦~',
          content: '请先登陆再查看更多内容',
          buttons: [{
            text: '暂不登录',
          }, {
            text: '立即登录',
            type: 'primary',
            onTap(e) {
              wx.navigateTo({
                url: '/pages/login/accredit/accredit',
              })
            }
          }],
        })
      }
    })
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})